<?php session_start(); ?>
<?php
include('../ketnoi.php');
include_once("fckeditor/fckeditor.php") ;
include ("../smtp.php");
if (!$_SESSION['user']) header("location: index.php"); // nếu biến session['banguyen'] chưa có thì trở về trang đăng nhập, copy dòng này vào trang nào muốn bảo vệ bằng mật khẩu
?>
<style type="text/css">
<!--
.error, .notice, .success {padding:.8em;margin:auto; margin-bottom:.5em;border:2px solid #AAF;width:570px;text-align: center}
.error {background: #ECECFF;color: red;border-color:#AAF;}
-->
</style>
<script type="text/javascript" src="scripts/joomla.js"></script>
<div class="m">
<?php 
    $shop=$dulieu;
	$trangthai = "";
	if($_GET['action']=='view' && $_GET['option']=='com_contact')
	{
		$sql = "SELECT * FROM `".$shop."`.`hoidap` order by MaCH desc";
		$ketqua = mysql_query($sql) or die ('khong tim thay du lieu');
		$tong=mysql_num_rows($ketqua);
		$_SESSION['limit']=$_POST['limit'];
		if($_SESSION['limit']==""){$sotrang = ceil($tong/20);}else{ 
		$sotrang = ceil($tong/$_SESSION['limit']);}
	    $dem = -1;
		
	if(isset($_POST['trang'])){
	 $trang = $_POST['trang'];
	}else{	
     if(isset($_POST['end'])){
	  $trang =$sotrang;
	 }else{
	  if(isset($_POST['start'])){
	   $trang =1;
	  }else{
	   $trang=1;
	  }
	 }	
	}
?>
<div id="menu">
	<div style="float: right;">
	<div class="icon1">
<a  class="toolbar" href="index.php"><img src="images/toolbar/icon-32-cancel.png" alt="Cencel"><br><span>Cancel</span></a>
	</div>
	</div>

	
</div>
<div class="header" style="background: url(images/dashboard/icon-48-generic.png) no-repeat scroll 0% 0% transparent; text-indent: 30px; line-height: 50px;">
  <h2 style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;Danh sách các câu hỏi</h2>
</div><br /><br />
<form name="adminForm" action="" method="post">
					
					<input name="option" value="com_contact" type="hidden">
					<input name="boxchecked" type="hidden">
					<input name="task" value="" type="hidden">
					<input name="func" value="" type="hidden">
<table width="100%"><tr>
<td width="30%">Lọc 
					  <select name="trangthai" id="filter_state" class="inputbox" size="1" onchange="adminForm.submit();">
					  <option value="" selected="selected">- Chọn trạng thái -</option>
					  <option value="1">Đã trả lời</option>
					  <option value="0">Chưa trả lời</option>
                      <option value="">Tất cả</option>
					  </select>	</td>
<td width="70%">
				<input class="inputbox" size="25" name="keyword" type="text">
				<input class="button_1" name="search" value="Tìm kiếm" type="submit">
</td></tr>
<tr>
<td colspan="2">
</td>
</tr>
</table>
<?php
 $keyword = $_POST['keyword'];
 $trangthai = $_POST['trangthai'];
if($keyword != ""){
         $query = "select * from `".$shop."`.`hoidap` where TieuDe like '%".$keyword."%' or CauHoi like '%".$keyword."%' or NoiDungTL like '%".$keyword."%' ";
		 $ketqua = mysql_query($query) or die ('khong tim thay du lieu'); 
		 $tong1=mysql_num_rows($ketqua);
 }
if($trangthai != ""){
	  if($trangthai ==0){
         $query = "select * from `".$shop."`.`hoidap` where NoiDungTL = ' '";
		 $ketqua = mysql_query($query) or die ('khong tim thay du lieu');
		 $tong1=mysql_num_rows($ketqua);
      }
	  
	  if($trangthai ==1){
		 $query = "select * from `".$shop."`.`hoidap` where NoiDungTL != ' '";
		 $ketqua = mysql_query($query) or die ('khong tim thay du lieu');
		 $tong1=mysql_num_rows($ketqua);
	  }
 }
?>
<table class="adminlist" width="100%">
<tbody><tr><th width="3%" class="title">#</th>
<th width="3%" class="title"><input name="toggle" value="" onclick="checkAll(<?php if($_SESSION['limit']==""){echo '20';}else{echo $_SESSION['limit']; } ?>)" type="checkbox"></th>
<th class="title" width="15%"> Tên người gởi</th>
<th class="title" width="17%"> Email</th>
<th class="title" width="11%"> Ngày gởi</th>
<th class="title" width="36%"> Tiêu đề </th>
<th class="title" width="11%"> Trả lời</th>  	 	 	 	 	 	
<th colspan="2" class="title" width="4%"> Xóa</th>
</tr>
 <?php 
	$stt=0;
	$t=0;
   while($dongDL = mysql_fetch_array($ketqua))
	  { 
	  $dem++;
	    if($_SESSION['limit']==""){$s=20;}
		else{$s=$_SESSION['limit'];}
		if($dem >= ($trang-1)*$s && $dem <= ($trang*$s)-1)
		{
  ?>
  
<tr class="row<?php echo $t ?>">
<td height="22"><?php echo $dem+1 ?></td>
<td align="center"><input id="cb<?php echo $stt ?>" name="product_id[<?php echo $dongDL['MaCH']?>]" value="<?php echo $dongDL['MaCH']?>" onclick="isChecked(this.checked);" type="checkbox"></td>
<td><?php echo $dongDL['TenNH']?></td>
<td><?php echo $dongDL['Email']?></td>
<td><?php 
    echo substr($dongDL['NgayHoi'],8,2)."-".substr($dongDL['NgayHoi'],5,2)."-".substr($dongDL['NgayHoi'],0,4).' &nbsp;'; 
	echo substr($dongDL['NgayHoi'],11,2);
	echo substr($dongDL['NgayHoi'],13,3);
?></td>
<td><?php echo $dongDL['TieuDe']?></td>

<td align="center"><?php if($dongDL['NoiDungTL']==''){?><a href="?option=com_contact&action=update&amp;id=<?php echo $dongDL['MaCH'] ?>">Trả lời</a> <?php ; }else{ ?><a href="?option=com_contact&action=xem&amp;id=<?php echo $dongDL['MaCH'] ?>">Xem trả lời<?php ;}?></a></td>

<td colspan="2"><a class="toolbar" href="?option=com_contact&action=delete&amp;id=<?php echo $dongDL['MaCH'] ?>" onclick="return confirm('Bạn có chắc chắn muốn xóa?');"><img src="images/delete_f2.png" width="20" height="20"alt="Delete" name="delete<?php echo $stt+1 ?>" align="middle" border="0"></a></td>
</tr>
	 <?php
	 $stt=$stt+1;
	 if($t==0)$t=1;
	 else $t=0;
	 }
	  }//ket thuc ham while in du lieu
	  ?>
</tbody></table>

<table class="adminlist"><tbody>
<?php if($sotrang > 1){?>
<tr>
  <td colspan="3" align="center"> 
  <input name="start" class="button2" type="submit" value="Start"/>
  <?php
for($i=1; $i<=$sotrang; $i++)
{ ?>
	<input name="trang" class="button" type="submit" value="<?php echo $i;?>"/>
<?php 
}
?>
<input name="end" class="button2" type="submit" value="End"/></td></tr>
<?php } ?>
<?php 
if($keyword=="" && $trangthai == ""){
?>
<tr><td width="48%" align="right" nowrap="nowrap">Display #</td><td>
<select class="inputbox" name="limit" size="1" onchange="this.form.submit();">
<option value="5"  <?php if($_SESSION['limit']==5) echo "selected='selected'" ?>>5</option>
<option value="10" <?php if($_SESSION['limit']==10) echo "selected='selected'" ?>>10</option>
<option value="15" <?php if($_SESSION['limit']==15) echo "selected='selected'" ?>>15</option>
<option value="20" <?php if($_SESSION['limit']=='') echo "selected='selected'" ?>>20</option>
<option value="25" <?php if($_SESSION['limit']==25) echo "selected='selected'" ?>>25</option>
<option value="30" <?php if($_SESSION['limit']==30) echo "selected='selected'" ?>>30</option>
<option value="50" <?php if($_SESSION['limit']==35) echo "selected='selected'" ?>>50</option>
</select>
</td><td width="48%" align="left" nowrap="nowrap">Results 1 - 
<?php if($_SESSION['limit']==''){if($tong < 20 ){echo $tong;}else echo '20';} else{if($_SESSION['limit'] > $tong ){echo $tong;}else echo $_SESSION['limit'];}?> of <?php echo $tong ?></td></tr>
<?php }else{
?>
<tr><td width="48%" align="right" nowrap="nowrap">Display #</td><td>
<select class="inputbox" name="limit" size="1" disabled="disabled">
<option><?php echo $tong1; ?></option>
</select>
</td><td width="48%" align="left" nowrap="nowrap"><?php if($tong1 >0){?>Results 1 - 
<?php echo $tong1;?> of <?php echo $tong1; }?></td></tr>
<?php
}
?>
</tbody></table>
</form>
<?php
}//ket thuc view

if($_GET['action']=='delete' && $_GET['option']=='com_contact')
	{
		  $sql = "Delete from `".$shop."`.`hoidap` where MaCH = '".$_GET['id']."'";
		  $delete = mysql_query($sql);
          echo"<script language=javascript>window.location='index.php?option=com_contact&action=view';</script>";
	}// ket thuc kiem tra action Delete
	  

if($_GET['action']=='update' && $_GET['option']=='com_contact')
{
		$sql ="SELECT * from `".$shop."`.`hoidap` where MaCH = '".$_GET['id']."'";
		$ketqua = mysql_query($sql);
		$dulieu = mysql_fetch_array($ketqua);
		
if(isset($_POST['email'])){  
if ( isset( $_POST ) )
   $postArray = &$_POST ;			// 4.1.0 or later, use $_POST
else
   $postArray = &$HTTP_POST_VARS ;	// prior to 4.1.0, use HTTP_POST_VARS	

$stt=0;
while (list($sForm,$value)=each($postArray))
{
$stt++;
if ( get_magic_quotes_gpc() )
	$postedValue = htmlspecialchars( stripslashes( $value ) ) ;
else
	$postedValue = htmlspecialchars( $value ) ;
$b[$stt]=$postedValue;
}	
 ?>
 	<form  name="form1" method="post">
    <input name="txt1" type="hidden"  id="txt1" value="<?=$b[1]?>" />
    <input name="txt2"  type="hidden" id="txt2" value="<?=$b[2]?>" />
    <input name="txt3" type="hidden" id="txt3" value="<?=$b[3]?>"/>
    <input name="txt4" type="hidden" id="txt4" value="<?=$b[4]?>"/>
	<input name="txt5"  type="hidden" id="txt5" value="<?=$b[5]?>"/>
    <input name="txt6"  type="hidden" id="txt5" value="<?=$b[6]?>"/>
	</form>
	
	<script language="JavaScript" type="text/javascript">
 		document.form1.submit();
    </script> 
 <?php

} 
 if ( isset( $_POST['txt1'] ) ){
  $error="";
  if($_POST['txt6']==""){
	  $error="Vui lòng nhập nội dung trả lời.";
  }else{	
    $query22 = "select * from  `".$shop."`.`trungtam` ";
    $kq22 = mysql_query($query22);
    $row22 = mysql_fetch_array($kq22);

    $name = $_POST['txt1'];
    $from = $row22['mail'];
    $to =  $_POST["txt2"];
    $subject = $row22['web']." - Trả lời câu hỏi";
    $message = "<b>".$_POST['txt4']."<b><br><i>".$_POST["txt5"]."<i><br><br>Chào ".$name."!<br>".$_POST["txt6"]."<br><br> Cám ơn vì đã quan tâm đến website ".$row22['web']."<br>".$row22['Tentv'];
    $header = "From: ".$from."\r\nReply-to: ".$from;
    SendMail($from,$to,$subject,$message,$row22['web']); 
	
    $sql = "UPDATE `".$shop."`.`hoidap` SET  `NoiDungTL` = '".$_POST['txt6']."',`NgayTL` = now() WHERE `hoidap`.`MaCH` = '".$_GET['id']."'";
    $update = mysql_query($sql) or die($sql);
	
  }
        
}

		
	?>
<div id="menu">
	<div style="float: right;">
	<div class="icon1">
<a  class="toolbar" href="?option=com_contact&action=view"><img src="images/toolbar/icon-32-cancel.png" alt="Remove"><br><span>Cancel</span></a>
	</div>
	</div>
	
	<div style="float: right;">
	<div class="icon1">
<a class="toolbar" onclick="adminForm3.submit();"><img src="images/toolbar/icon-32-save.png"  alt="New"><br><span>Save</span></a>
	</div>
	</div>
</div>

<div class="header" style="background: url(images/dashboard/icon-48-generic.png) no-repeat scroll 0% 0% transparent; text-indent: 30px; line-height: 50px;">
  <h2 style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;Trả lời cau hỏi</h2>
</div><br /><br />
<?php
	if($error!=""){
	?>
	 <div class="error" align="center"><img src='../images/cancel_f2.png' width='10px' height='10px'/> <?php echo $error ?></div>
  <?php
  }
?>
		<form name="adminForm3" action="" method="post" enctype="multipart/form-data">

<table width="1000" class="adminform">
	<tbody><tr> 
   		<td width="400" valign="top">
        <fieldset>
			  <legend>Thông tin	người gởi </legend>
			  <table width="100%" border="0">
      			<tbody>
    			<tr class="row1"> 
      				<td width="18%"><div style="text-align: right; font-weight: bold;">
				  Họ tên:</div>      				</td>
      				<td width="82%"> 
       				   <input readonly="readonly" style="margin-left:10px; width:250px" name="ten" id="ten" size="30" type="text" value="<?php echo $dulieu['TenNH'] ?>"> </td>
    			</tr>
    		
				<tr class="row0"> 
      				<td width="18%"><div style="text-align: right; font-weight: bold;">
				  Email:</div>      				</td>
   				  <td width="82%"> <input readonly="readonly" style="margin-left:10px; width:250px" name="email" id="email" size="30" type="text" value="<?php echo $dulieu['Email'] ?>"></td>
    			</tr>
                <tr class="row1"> 
      				<td width="18%"><div style="text-align: right; font-weight: bold;">
				  Số điện thoại:</div>      				</td>
   				  <td width="82%"> <input readonly="readonly" style="margin-left:10px; width:250px" name="sdt" id="email" size="30" type="text" value="<?php echo $dulieu['SDT'] ?>"></td>
    			</tr>
				<tr class="row0"> 
				  <td width="18%"><div style="text-align: right; font-weight: bold;">Ngày gởi:</div>      				</td>
				  <td width="82%" style="padding-left:10px;">&nbsp;&nbsp;
  <?php 
    echo substr($dulieu['NgayHoi'],8,2)."-".substr($dulieu['NgayHoi'],5,2)."-".substr($dulieu['NgayHoi'],0,4).' &nbsp;'; 
?>					</td>
				  </tr>
    			  <tr class="row0"> 
    					    
		      		<td valign="top" width="18%">&nbsp;</td>
		      		<td width="82%">&nbsp;</td>		    
    			  </tr>
	  			  </tbody></table></fieldset>
 		</td>
        
        <td width="400" valign="top">
        <fieldset>
			  <legend>Câu hỏi
			</legend><table width="100%" border="0">
      			<tbody>
				 <tr class="row1"> 
				   <td width="28%"><div style="text-align: right; font-weight: bold;">Tiêu đề:</div>      				</td>
				   <td width="72%"><input readonly="readonly" style="margin-left:10px; width:250px" name="TieuDe" id="TieuDe" size="30" type="text" value="<?php echo $dulieu['TieuDe'] ?>"></td>
			      </tr>
				<tr class="row0"> 
   				  <td width="28%" valign="top"><div style="text-align: right; font-weight: bold;">Nội dung câu hỏi:</div>      				</td>
      				<td width="72%"><textarea readonly="readonly" style="margin-left:10px; width:250px" name="noidung" cols="40" rows="6"><?php echo $dulieu['CauHoi'] ?></textarea></td>
    			</tr>
    			  <tr class="row0"> 
    					    
		      		<td valign="top" width="28%">&nbsp;</td>
		      		<td width="72%">&nbsp;</td>		    
    			  </tr>
	  			  </tbody></table></fieldset>
 		</td>
  	</tr>
</tbody></table>
<p></p>	    
<table style=" padding-left:10px" border="0" cellpadding="0" cellspacing="0" class="adminform">
<tr>
<td width="100px" style="padding-left:10px; font-weight:bold" height="35" valign="top"> Nội dung trả lời </td>
<td width="887px">
<?php
		$oFCKeditor = new FCKeditor('FCKeditor1') ;
		$oFCKeditor->BasePath = '/fckeditor/' ;
		$oFCKeditor->Value = 'Nội dung trả lời';
		$oFCKeditor->Create() ;
	?>
   </td></tr></table>
</form>
<?php
}//ket thuc tra loi



if($_GET['action']=='xem' && $_GET['option']=='com_contact')
	{

		$_SESSION['ma']= $_GET['id'];
		$sql ="SELECT * from `".$shop."`.`hoidap` where MaCH = '".$_GET['id']."'";
		$ketqua = mysql_query($sql);
		$dulieu = mysql_fetch_array($ketqua);
	?>
<div id="menu">
	<div style="float: right;">
	<div class="icon1">
<a  class="toolbar" href="?option=com_contact&action=view"><img src="images/toolbar/icon-32-cancel.png" alt="Remove"><br><span>Cancel</span></a>
	</div>
	</div>

</div>

<div class="header" style="background: url(images/dashboard/icon-48-generic.png) no-repeat scroll 0% 0% transparent; text-indent: 30px; line-height: 50px;">
  <h2 style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;Xem trả lời câu hỏi </h2>
</div><br /><br />
		<form name="adminForm3" action="" method="post" enctype="multipart/form-data">

<table width="1000" class="adminform">
	<tbody><tr> 
   		<td width="400" valign="top">
        <fieldset>
			  <legend>Thông tin	người gởi</legend>
			  <table width="100%" border="0">
      			<tbody>
    			<tr class="row1"> 
      				<td width="22%"><div style="text-align: right; font-weight: bold;">
				  Họ tên:</div>      				</td>
      				<td width="78%"> 
       				   <input readonly="readonly" style="margin-left:10px; width:250px" name="ten" id="ten" size="30" type="text" value="<?php echo $dulieu['TenNH'] ?>"> </td>
    			</tr>
    		
				<tr class="row0"> 
      				<td width="22%"><div style="text-align: right; font-weight: bold;">
				  Email:</div>      				</td>
   				  <td width="78%"> <input readonly="readonly" style="margin-left:10px; width:250px" name="email" id="email" size="30" type="text" value="<?php echo $dulieu['Email'] ?>"></td>
    			</tr>
                <tr class="row1"> 
      				<td width="22%"><div style="text-align: right; font-weight: bold;">
				  Số điện thoại:</div>      				</td>
   				  <td width="78%"> <input readonly="readonly" style="margin-left:10px; width:250px" name="sdt" id="email" size="30" type="text" value="<?php echo $dulieu['SDT'] ?>"></td>
    			</tr>
				<tr class="row0"> 
				  <td width="22%"><div style="text-align: right; font-weight: bold;">Ngày gởi:</div>      				</td>
				  <td width="78%" style="padding-left:10px;">&nbsp;&nbsp;
  <?php 
    echo substr($dulieu['NgayHoi'],8,2)."-".substr($dulieu['NgayHoi'],5,2)."-".substr($dulieu['NgayHoi'],0,4).' &nbsp;'; 
?>					</td>
				  </tr>
                 <tr class="row0"> 
				  <td width="22%"><div style="text-align: right; font-weight: bold;">Ngày trả lời:</div>      				</td>
				  <td width="78%" style="padding-left:10px;">&nbsp;&nbsp;
  <?php 
    echo substr($dulieu['NgayTL'],8,2)."-".substr($dulieu['NgayTL'],5,2)."-".substr($dulieu['NgayTL'],0,4).' &nbsp;'; 
?>					</td>
				  </tr>
    			  <tr class="row0"> 
    					    
		      		<td valign="top" width="22%">&nbsp;</td>
		      		<td width="78%">&nbsp;</td>		    
    			  </tr>
	  			  </tbody></table></fieldset>
 		</td>
        
        <td width="400" valign="top">
        <fieldset>
			  <legend>Ý kiến</legend>
			<table width="100%" border="0">
      			<tbody>
				 <tr class="row1"> 
				   <td width="28%"><div style="text-align: right; font-weight: bold;">Tiêu đề:</div>      				</td>
				   <td width="72%"><input readonly="readonly" style="margin-left:10px; width:250px" name="TieuDe" id="TieuDe" size="30" type="text" value="<?php echo $dulieu['TieuDe'] ?>"></td>
			      </tr>
				<tr class="row0"> 
   				  <td width="28%" valign="top"><div style="text-align: right; font-weight: bold;">Nội dung ý kiến:</div>      				</td>
      				<td width="72%"><textarea readonly="readonly" style="margin-left:10px; width:250px" name="noidung" cols="40" rows="6"><?php echo $dulieu['CauHoi'] ?></textarea></td>
    			</tr>
    			  <tr class="row0"> 
    					    
		      		<td valign="top" width="28%">&nbsp;</td>
		      		<td width="72%">&nbsp;</td>		    
    			  </tr>
	  			  </tbody></table></fieldset>
 		</td>
  	</tr>
</tbody></table>
<p></p>	    
<table style=" padding-left:10px" border="0" cellpadding="0" cellspacing="0" class="adminform">
<tr>
<td width="100px" style="padding-left:10px; font-weight:bold" height="35" valign="top"> Nội dung trả lời </td>
<td width="887px">
<?php
		$oFCKeditor = new FCKeditor('FCKeditor1') ;
		$oFCKeditor->BasePath = '/fckeditor/' ;
		$oFCKeditor->Value =  $dulieu['NoiDungTL'];
		$oFCKeditor->Create() ;
	?>
   </td></tr></table>
</form>
<?php
}//ket thuc xem
?>
</div>
